<?php
namespace Home\Controller;
use Think\Controller;
class UserController extends Controller {
   //公共的方法 
    public function Verify(){
        //验证码
      $Verify = new \Think\Verify();
      $Verify->fontSize = 15;
      $Verify->length   = 4;
      $Verify->entry();
    }
    public function checkName($uname){
        $uname=I('get.uname');
        $res=M('user')->where(array('uname'=>$uname))->find();
        $re=array();
        if(!$res){
            $re['msg']='用户名可用';
            $re['status']=true;     
        }else{
            $re['msg']='用户名已存在!';
            $re['status']=false;    
        }
        $this->ajaxReturn($re);

    }

    public function checkpwd(){
        $pwd=I('get.pwd');
        $uname=I('get.uname');
        $pwd=md5($pwd);
        $res=M('user')->where(array('uname'=>$uname,'pwd'=>"$pwd"))->find();
        $re=array();
        if($res){
            $re['msg']='密码正确!';
            $re['status']=true;     
        }else{
            $re['msg']='密码不正确,请重新输入!';
            $re['status']=false;    
        }
        $this->ajaxReturn($re);

    }
    public function checkcode(){
        $Verify = new \Think\Verify();
        $re=array();
        if($Verify->check(I('get.code'))){
            $re['msg']='验证正确!';
            $re['status']=true;     
        }else{
            $re['msg']='验证码不正确!';
            $re['status']=false;    
        }
        $this->ajaxReturn($re);
    }

  //处理方法  
    public function index(){     
        $this->display();
    }
    public function register(){
      $this->display();
    }
    public function doRegister(){
        $act=MD5(time().rand(1111,9999));
        $user=D('user');
        $_POST['act']=$act;
        if($user->create($_POST)){            
        $user->add();
          mymail(I('post.email'),'魅族商城','魅族商城注册激活码','http://127.0.0.1'.U('jh',array('act'=>$act)));

        $this->success('注册成功,请及时前往邮箱完成注册码激活!',U('login'),2);
      }else{
        $this->error($user->getError(),U('register'),3);
        exit;
      }
    }
    public function login(){
        $this->display();
    }
    public function doLogin(){
        $uname=I('post.uname');
        $pwd=md5(I('post.pwd'));
        $res=M('user')->where(array('uname'=>$uname,'pwd'=>$pwd))->find();
        if($res){
            if($res['status']!=2){
               $this->error('您的账号尚未激活,请及时前往邮箱完成注册码激活',U('Home/Index/index'),2); 
               exit;
            }
            $uid=$res['uid'];
            session('username',$uname);
            session('photo',$res['photo']);
            session('uid',$uid);
            session('userInfo',$res);
            $this->success('登录成功',U('Home/Index/index'),2);
        }else{
            $this->error('登录失败',U('login'),2);
        }
    }
    public function logout(){
        session('username',null);
        session('photo',null);
        session('uid',null);
        session('userInfo',null);
        session('total',null);
        session('payorder',null);        
        $this->success('注销成功',U('Home/Index/index'),2);
        
    }
    public function jh(){
        $act=I('get.act');
        $_POST['status']=2;
        $res=M('user')->where(array('act'=>$act))->find();

        if($res['status']==2){
            $this->error('此账号已经激活,请勿重复!',U('login'),2);
            exit;
        }
        if($res){
            M('user')->where(array('act'=>$act))->save($_POST);
            $this->success('账号激活成功',U('Home/Index/index'),2);                
        }else{
            $this->error('账号激活失败,请重新注册',U('register'),2);
        }
        $this->assign('act',$act);
        $this->display();
    }  




}